using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksBarcodeRelRecService), ServiceLifetime = LifeTime.Transient)]
    public class CksBarcodeRelRecService : BaseService<CksBarcodeRelationshipRecord>, ICksBarcodeRelRecService
    {
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksBarcodeRelRecDto> GetList(CksBarcodeRelRecQueryDto parm)
        {
            var response = Queryable()
                .LeftJoin<PlcConfig>((a, b) => a.PlcID == b.Id)
                .WhereIF(!string.IsNullOrEmpty(parm.PrimaryCode), (a,b) => a.PrimaryCode.Contains(parm.PrimaryCode))
                .WhereIF(!string.IsNullOrEmpty(parm.SubcodeCode), (a,b) => a.SubcodeCode.Contains(parm.SubcodeCode))
                .OrderByDescending((a, b) => a.CreatedTime)
                .Select((a, b) => new CksBarcodeRelRecDto {
                    Id = a.Id,
                    PlcID = a.PlcID,
                    CreatedTime = a.CreatedTime,
                    IsDeleted = a.IsDeleted,
                    LineCode = b.ProductionLineCode,
                    PrimaryCode = a.PrimaryCode,
                    ReadDB = a.ReadDB,
                    StationCode = b.WorkstationCode,
                    SubcodeCode = a.SubcodeCode,
                    Type = (int)a.Type
                }).ToPage(parm);
            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksBarcodeRelationshipRecord GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksBarcodeRelationshipRecord AddCksBarcodeRelRec(CksBarcodeRelationshipRecord model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksBarcodeRelRec(CksBarcodeRelationshipRecord model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksBarcodeRelationshipRecord> QueryExp(CksBarcodeRelRecQueryDto parm)
        {
            var predicate = Expressionable.Create<CksBarcodeRelationshipRecord>();
            predicate.AndIF(!string.IsNullOrEmpty(parm.PrimaryCode), o => o.PrimaryCode.Contains(parm.PrimaryCode));
            predicate.AndIF(!string.IsNullOrEmpty(parm.SubcodeCode), o => o.SubcodeCode.Contains(parm.SubcodeCode));
            return predicate;
        }
    }
}